// pages/Index/shopCustomization/shopCustomization.js
const controller=require("../../controllers/Controller").controller
const controllerIndex=require("../../controllers/controllerIndex").controller

Page({

  /**
   * 页面的初始数据
   */
  data: {
    detail:'',
    icon1:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452706747482112.png",
    icon2:"https://xinlingshoushangquan.oss-cn-shenzhen.aliyuncs.com/account/20210513/842452919725850624.png",
    selectIndex:"",
    selectIndex2:0,
    num:1,
    totalPrice:0,
    selectAll:false,
    canPay:true
  },
  //选中取消选中
  selectItem(e){
    let index=e.currentTarget.dataset.index
    if(this.data.Effective==true){
      if(this.data.selectIndex===0){
        this.setData({
          selectIndex:2
        })
      }else{
        this.setData({
          selectIndex:0
        })
      }
    }else{
      if(this.data.selectIndex===1){
        this.setData({
          selectIndex:2
        })
      }else{
        this.setData({
          selectIndex:1
        })
      }
    }
    this.totalPrice()
    this.judgeSelectAll()
  },
  //选择最后一项
  selectItem2(){
    if(this.data.selectIndex2===0){
      this.setData({
        selectIndex2:1
      })
    }else{
      this.setData({
        selectIndex2:0,
      })
    }
    this.totalPrice()
    this.judgeSelectAll()
  },

  //判断是否为全选
  judgeSelectAll(){
    if((this.data.selectIndex===0||this.data.selectIndex===1)&&this.data.selectIndex2===1){
      this.setData({
        selectAll:true
      })
    }else{
      this.setData({
        selectAll:false
      })
    }
  },

  //获取定制店铺费用
  getShopCustomization(){
    controller.getDesignShop().then(res=>{
      this.setData({
        detail:res.data
      })
      this.totalPrice()
    })
  },

  //增加
  add(){
    this.setData({
      num:this.data.num+1
    })
    this.totalPrice()
  },

  //减少
  reduce(){
    let num=this.data.num
    if(num>1){
      this.setData({
        num:num-1
      })
    }
    this.totalPrice()
  },
  //计算价格
  totalPrice(){
    let price=0
    let num=0
    if(this.data.selectIndex===0){
      price=price+this.data.detail.discountPrice
      num=1
    }else if(this.data.selectIndex===1){
      price=price+this.data.detail.price
      num=1
    }

    if(this.data.selectIndex2==1){
      price=price+this.data.detail.unitPrice*this.data.num
      num=num+this.data.num
    }
    this.setData({
      totalPrice:price.toFixed(2),
      totalNum:num
    })
  },

  //获取店铺详情
  getShopDetail(){
    controllerIndex.shopInfo({
      sessionId: wx.getStorageSync('token'),
      shopId:wx.getStorageSync('shopId')
    }).then(res=>{
      let newTime=new Date().getTime();
      if(res.data.shopGroupEndTime){
        if(new Date(res.data.shopGroupEndTime).getTime()>newTime){
          this.setData({
            Effective:true
          })
          wx.setStorageSync('Effective', true)
        }else{
          this.setData({
            Effective:false
          })
          wx.setStorageSync('Effective', false)
        }
      }else{
        this.setData({
          Effective:false
        })
        wx.setStorageSync('Effective', false)
      }
      // if(this.data.Effective==true){
      //   this.setData({
      //     selectIndex:0
      //   })
      // }else{
      //   this.setData({
      //     selectIndex:1
      //   })
      // }
    })
  },

  //全选
  selectAll(){
    if(this.data.selectAll==false){
      this.setData({
        selectAll:true
      })
      if(this.data.Effective==true){
        this.setData({
          selectIndex:0,
          selectIndex2:1
        })
      }else{
        this.setData({
          selectIndex:1,
          selectIndex2:1
        })
      }
    }else{
      this.setData({
        selectAll:false
      })
      this.setData({
        selectIndex:2,
        selectIndex2:0
      })
    }
    this.totalPrice()
  },

  //创建订单
  createOrder(){
    if(this.data.canPay==false){
      return
    }
    this.setData({
      canPay:false
    })
    let that=this
    let value={}
    value.designShopId=this.data.detail.id
    value.shopId=wx.getStorageSync("shopId")
    if(this.data.selectIndex2==1){
      value.unitNum=this.data.num
    }else{
      value.unitNum=0
    }
    if(this.data.selectIndex===1){
      value.payDesignShop=1
    }else if(this.data.selectIndex===0){
      value.payDesignShop=1
    }else{
      value.payDesignShop=0
    }
    controller.createDesignShopOrder(value).then(res=>{
      let orderNo = res.data.orderNo
      let accountId=res.data.accountId
      let orderId=res.data.id
      controllerIndex.weChatPay({
        shopId: wx.getStorageSync('shopId'),
        orderNo:orderNo,
        frontUrl:"mp:///pages/Index/shopCustomization/shopCustomization",
        type:"SP",
        openId:wx.getStorageSync('openId')
      }).then(res =>{
        console.log(res);
        // wx.navigateTo({
        //   url: '/pages/weixinlink/weixinlink?url='+encodeURIComponent(res.data)
        // })
        let data=JSON.parse(res.data);
        console.log(data)
        wx.requestPayment({
          nonceStr: data.nonceStr,
          package: data.package,
          paySign: data.paySign,
          timeStamp: data.timeStamp,
          signType: data.signType,
          success(res){
            console.log(res)
          },
          fail(res){
            console.log(res)
          },
          complete(res){
            that.setData({
              canPay:true
            })
          }
        })
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      shopId: wx.getStorageSync('shopId'),
      endTime: wx.getStorageSync('endTime').slice(0,10),
      Effective: JSON.parse(wx.getStorageSync('Effective'))
    })
    this.getShopCustomization()
    this.getShopDetail()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})